package com.fanrui.code.player;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.ibatis.session.SqlSession;

import com.fanrui.code.db.jvmdb.JvmDao;
import com.fanrui.code.entity.User;
import com.fanrui.code.entity.UserMapper;
import com.fanrui.code.thread.MainLoopPlus.StrongRunnable;
import com.fanrui.code.util.SessionUtil;
import com.fanrui.code.util.core.LoopThreadPool;
import com.fanrui.code.util.logger.Stdout;

public class PlayerDao extends JvmDao<Player, Long> {

	// singleton
	private PlayerDao() {
		super(Player.class);
		LoopThreadPool.getInstance().scheduleAtFixedRate(new StrongRunnable() {

			@Override
			protected void strongRun() throws Exception {
				updateToDB();
			}

		}, 5, 5, TimeUnit.SECONDS);
	}

	private static class PlayerDaoSingleton {
		private static PlayerDao instance = new PlayerDao();
	}

	public static PlayerDao getInstance() {
		return PlayerDaoSingleton.instance;
	}
	
	/**
	 * 更新数据
	 */
	protected void updateToDB() {
		if(modifiedEntities != null && !modifiedEntities.isEmpty()) {
			synchronized (modifiedEntities) {
				Stdout.GET().info("User:定时更新数据到数据库!此次更新{}条数据",modifiedEntities.size());
				SqlSession session = SessionUtil.getSession();
				UserMapper um = session.getMapper(UserMapper.class);
				for(Player player : modifiedEntities.keySet()) {
					um.updateByPrimaryKey(player.toUser());
				}
				modifiedEntities.clear();
				session.commit();
				session.close();
			}
		}
	}

	@Override
	protected void OtherSave4jvm(Player entity) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected Collection<Player> dbFindAll() {
		SqlSession session = SessionUtil.getSession();
		UserMapper um = session.getMapper(UserMapper.class);
		Collection<User> userDatas = um.selectAllUser(); // 数据库中的玩家信息
		session.close();
		List<Player> result = new ArrayList<Player>();
		if(userDatas != null && !userDatas.isEmpty()){
			for(User u:userDatas){
				result.add(loadPlayer(u));
			}
		}
		Stdout.GET().info("{}个玩家被加载到内存中",result.size());
		return result;
	}

	/**
	 * 加载用户实名注册信息
	 * @param u
	 * @return
	 */
	private Player loadPlayer(User u) {
		Player player = new Player(u);
		return player;
		
//		try (SqlSession session = SessionUtil.getSession()){
//			CertificationMapper mapper = session.getMapper(CertificationMapper.class);
//			Certification certification = mapper.selectByUserId(u.getId());
//			if(certification != null){
//				VerifiedCmd.Builder builder = VerifiedCmd.newBuilder();
//				builder.setIDCard(certification.getCardNo());
//				builder.setName(certification.getRealName());
//				player.setVerified(builder.build());
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
	}

	@Override
	protected void dbSave(Player entity) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void otherDelete4Jvm(Player entity) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void dbDelete(Long id) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * 创建一个玩家并加载到内存中
	 * @param playerId
	 * @return
	 */
	public Player createPlayerAndLoad(Long playerId) {
		if(playerId != null){
			SqlSession session = SessionUtil.getSession();
			UserMapper um = session.getMapper(UserMapper.class);
			User user = um.selectByPrimaryKey(playerId);
			session.close();
			
			// 数据库中存在的用户
			if(user != null){
				Player player = create(playerId);
				player.load(user);
				return player;
			}
		}
		return null;
	}
	/**
	 * 加载所有玩家的家族
	 * */
	public void loadAllPlayerFamily(){
		for(Player player:entities.values()){
			player.loadFamily();
		}
	}
	
	/**
	 * 加载所有玩家的俱乐部
	 */
	public void loadAllPlayerClubs() {
		for(Player player:entities.values()){
			player.loadClubs();
		}
	}
}
